<template>
  <a-modal :title="title" style="top: 100px;" :visible="true" width="1300px" @cancel="handleCancel">
    <div class="tips">
    </div>
    <a-table class="product-table" :rowKey="row => row.id" :dataSource="list" bordered :pagination="false" size="small">
      <a-table-column title="标题" width="120px">
        <template slot-scope="row">
          <div>{{row.title}}</div>
        </template>
      </a-table-column>
      <a-table-column title="数量" width="30px">
        <template slot-scope="row">
          <div>{{row.num}}</div>
        </template>
      </a-table-column>
      <a-table-column title="货物总价" width="50px">
        <template slot-scope="row">
          <div>{{row.money_price}}</div>
        </template>
      </a-table-column>
      <a-table-column title="货运公司" width="60px">
        <template slot-scope="row">
          <div>{{row.express_name}}</div>
        </template>
      </a-table-column>
      <a-table-column title="货运单号" width="60px">
        <template slot-scope="row">
          <div>{{row.express_number}}</div>
        </template>
      </a-table-column>
      <a-table-column title="备注" width="60px">
        <template slot-scope="row">
          <div>{{row.remark}}</div>
        </template>
      </a-table-column>
      <a-table-column title="状态" width="60px">
        <template slot-scope="row">
          <div v-if="row.status == 1">物流中</div>
          <div v-else-if="row.status == 2">已入库</div>
          <div v-else-if="row.status == 3">已取消</div>
          <div v-else>已下单</div>
        </template>
      </a-table-column>
      <a-table-column title="创建时间" width="60px">
        <template slot-scope="row">
         <span>{{row.created_at}}</span>
        </template>
      </a-table-column>
      <a-table-column title="更新时间" width="60px">
        <template slot-scope="row">
         <span>{{row.updated_at}}</span>
        </template>
      </a-table-column>
      <a-table-column title="创建者" width="60px">
        <template slot-scope="row">
          <div>{{row.admin_name}}</div>
        </template>
      </a-table-column>
      <a-table-column title="结束者" width="60px">
        <template slot-scope="row">
          <div>{{row.admin_name2}}</div>
        </template>
      </a-table-column>
      <a-table-column title="操作" width="120px">
          <template slot-scope="row"">
          <a href="javascript:;" @click="handleEdit(row,1)" v-if="row.status === 1 && action_show">修改</a>
          <a-divider type="vertical" />
          <a href="javascript:;" @click="handleEdit(row,2)" v-if="row.status === 1 && action_show">入库</a>
          <a-divider type="vertical" />
          <a href="javascript:;" @click="handleEdit(row,3)" v-if="row.status === 1 && action_show">取消</a>
        </template>
      </a-table-column>
    </a-table>
    <template slot="footer">
      <div class="btn-c">
        <a-button type="primary" class="add" @click="handleCreate">
          新增一个采购
        </a-button>
        <div style="flex-grow: 1;"></div>
        <a-button key="back" @click="handleCancel">关闭</a-button>
      </div>
    </template>
  </a-modal>
</template>
<script>
  import UploadImages from '@/components/UploadImages/UploadImages'
  import iSwitch from '@/components/ISwitch/ISwitch'
  import ISkuType from '@/components/ISkuType/ISkuType'
  export default {
    components: {
      UploadImages,
      iSwitch,
      ISkuType,
    },
    props: {
      info: {
        type: Object,
        default () {
          return {}
        }
      }
    },
    data() {
      return {
        action_show: false,
        isEditTaskLog: false,
        participant: '',
        list: [],
        listWeight: '',
        frame: [
          {
            title: '标题',
            key: 'title',
            newline: true,
            required: true,
            default: this.info.title,
            disabled:true,
          },
          {
            title: '数量',
            key: 'num',
            type: 'number',
            newline: true,
            required: true,
          },
          {
            title: '货物总价',
            key: 'money_price',
            type: 'number',
            newline: true,
            required: true,
          },
          {
            title: '货运公司',
            key: 'express_name',
            newline: true,
          },
          {
            title: '货运单号',
            key: 'express_number',
            newline: true,
          },
          {
            title: '备注',
            type: 'textarea',
            key: 'remark',
            span: 22,
          },
          {
            title: '付款附件',
            type: 'image',
            key: 'image',
            showLibrary: false,
            required: true,
            span: 22,
          },
          {
            title: '状态',
            key: 'status',
            type: 'radio',
            default: 1,
            required: true,
            span: 24,
            options: [
              {
                title: '物流中',
                value: 1,
              },
            ],
          },
        ],
        frame2: [
          {
            title: '标题',
            key: 'title',
            newline: true,
            required: true,
            default: this.info.title,
            disabled:true,
          },
          {
            title: '数量',
            key: 'num',
            type: 'number',
            newline: true,
            required: true,
            disabled:true,
          },
          {
            title: '货物总价',
            key: 'money_price',
            type: 'number',
            newline: true,
            required: true,
            disabled:true,
          },
          {
            title: '签收附件',
            type: 'image',
            key: 'image2',
            showLibrary: false,
            span: 22,
          },
          {
            title: '状态',
            key: 'status',
            type: 'radio',
            default: 2,
            required: true,
            span: 24,
            options: [
              {
                title: '已入库',
                value: 2,
              }
            ],
          },
        ],
        frame3: [
          {
            title: '标题',
            key: 'title',
            newline: true,
            required: true,
            default: this.info.title,
            disabled:true,
          },
          {
            title: '数量',
            key: 'num',
            type: 'number',
            newline: true,
            required: true,
            disabled:true,
          },
          {
            title: '货物总价',
            key: 'money_price',
            type: 'number',
            newline: true,
            required: true,
            disabled:true,
          },
          {
            title: '取消附件',
            type: 'image',
            key: 'image2',
            showLibrary: false,
            required: true,
            span: 22,

          },
          {
            title: '状态',
            key: 'status',
            type: 'radio',
            default: 3,
            required: true,
            span: 24,
            options: [
              {
                title: '已取消',
                value: 3,
              }
            ],
            tips: '这里是取消,这里是取消,这里是取消',
          },

        ],
        frame_bf: [],
        row_bf: [],
      }
    },
    computed: {
      title() {
        return '编辑采购'
      }
    },
    watch: {
      uuid() {
        this.initList()
      },
    },
    created() {
      this.action_show = this.$authCheck('storage_buylist')
      this.initList()
    },
    methods: {
      initList() {
        this.$http.get(`/storage_buy`, {
          params: {
            xq_id: this.info.id,
            status:this.info.buys_status,
          }
        }).then(res => {
          this.list = res.data.list
        })
      },
      handleCreate(row) {
        this.$iForm.show({
          title: '新增',
          frame: this.frame,
          success: (data) => {
            this.$http.post(`/storage_buy`, {
              ...data,
              xq_id:this.info.id,
            }).then(res => {
              this.initList()
              this.$iForm.hide()
              this.$emit('refresh')
            })
          },
        })
      },
 
      handleEdit(row,status) {
        if(status == 2){
          this.frame_bf = this.frame2
          this.row_bf = Object.assign({}, row)
          this.row_bf.status = 2
        }else if(status == 3){
          this.frame_bf = this.frame3
          this.row_bf = Object.assign({}, row)
          this.row_bf.status = 3
        }else{
          this.frame_bf = this.frame
          this.row_bf = Object.assign({}, row)
        }
        this.$iForm.show({
          title: '编辑',

          frame: this.frame_bf,
          form: this.row_bf,
          success: (data) => {
            this.$http.put(`/storage_buy/${row.uuid}`, {
              type: 'update',
              attributes: {
                ...data
              }
            }).then(res => {
              this.initList()
              this.$iForm.hide()
              this.$emit('refresh')
            })
          },
        })
      },
      handleCancel() {
        this.$emit('close')
      },
      handleDelete(row) {
        this.$confirm({
          title: '提示',
          content: `确认删除吗？`,
          onOk: () => {
            this.$http.delete(`/storage_buy/${row.uuid}`).then(res => {
              this.$message.success('删除成功')
              this.initList()
            })

          }
        })


      }
    }
  }
</script>
<style lang="less" scoped>
  .thumb {
    width: 60px;
    // height: 60px;
    border-radius: 6px;
  }

  .btn-c {
    display: flex;
    // margin-top: 30px;
    padding: 0rpx 30rpx;

    .add {}

    .save {
      margin-right: 20px;
    }

    .cancel {}
  }

  .tips {
    color: gray;
    font-size: 80%;
    margin-bottom: 10px;
  }

  a-input {
    display: inline;
  }
</style>
